// @ts-ignore
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';

dayjs.locale('zh-cn')


 export const NumberFormat = (value) =>{
    if (!value) {
      return '0'
    }
     // 将整数部分逢三一断
    return value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
  }

export const dayformat= (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') => {
    return dayjs(dataStr).format(pattern)
  }

export  const dateFormat = (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') => {
    return dayjs(dataStr).format(pattern)
  }


  /**
   * 清理空值，对象
   * @param children
   * @returns {*[]}
   */
  export function filterEmpty (children = <object[]>[]) {
    // console.log('children',children)
    if (typeof children === 'object'){
      // @ts-ignore
        return children.filter(c => c.key || (c.text && c.text.trim() !== ''))
    }
    return <object[]>[]
  }

  /**
   * 获取字符串长度，英文字符 长度1，中文字符长度2
   * @param {*} str
   */
export  const getStrFullLength = (str = '') =>
    str.split('').reduce((pre, cur) => {
      const charCode = cur.charCodeAt(0)
      if (charCode >= 0 && charCode <= 128) {
        return pre + 1
      }
      return pre + 2
    }, 0)

  /**
   * 截取字符串，根据 maxLength 截取后返回
   * @param {*} str
   * @param {*} maxLength
   */
  export const cutStrByFullLength = (str = '', maxLength) => {
    let showLength = 0
    return str.split('').reduce((pre, cur) => {
      const charCode = cur.charCodeAt(0)
      if (charCode >= 0 && charCode <= 128) {
        showLength += 1
      } else {
        showLength += 2
      }
      if (showLength <= maxLength) {
        return pre + cur
      }
      return pre
    }, '')
  }

const userFormat= function() {
  return { NumberFormat, dayjs, dateFormat, filterEmpty, getStrFullLength, cutStrByFullLength }
}

export default  userFormat
